package com.bangcommunity.bbframe.mybatis.manager;

import com.bangcommunity.bbframe.common.base.PageBean;
import com.bangcommunity.bbframe.common.base.PageInfo;
import com.bangcommunity.bbframe.common.exception.ValidateUtil;
import com.bangcommunity.bbframe.common.utils.lang.CollectionUtils;
import com.bangcommunity.bbframe.mybatis.criteria.AbstractorCriteria;
import com.bangcommunity.bbframe.mybatis.mapper.BaseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 业务操作基类
 * 
 * @author tanghc
 */
public class BaseManager<PK extends Serializable, E extends Serializable, C extends AbstractorCriteria, D extends BaseMapper<PK, E, C>>
        implements IBaseManager<PK, E, C> {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected D baseMapper;

    @Autowired
    protected void setBaseDao(D baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public int countByExample(C example) {
        ValidateUtil.checkNotNull(example, "条件不能为空");
        return this.baseMapper.countByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByPrimaryKey(PK id) {
        ValidateUtil.checkNotNull(id, "数据id不能为空");
        ValidateUtil.isTrue(this.baseMapper.deleteByPrimaryKey(id) == 1, "删除数据有误");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByExample(C example) {
        ValidateUtil.checkNotNull(example, "条件不能为空");
        return this.baseMapper.deleteByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(E record) {
        ValidateUtil.checkNotNull(record, "保存数据不能为空");
        validateInsert(record);
        ValidateUtil.isTrue(this.baseMapper.insert(record) > 0, "保存数据有误");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchInsert(List<E> list) {
        ValidateUtil.isTrue(CollectionUtils.isNotEmpty(list), "批量添加不能为空");
        for (E e : list) {
            validateInsert(e);
        }
        ValidateUtil.isTrue(this.baseMapper.batchInsert(list) > 0, "批量保存数据有误");
    }

    @Override
    public List<E> selectByExample(C example) {
        ValidateUtil.checkNotNull(example, "条件不能为空");
        return this.baseMapper.selectByExample(example);
    }

    @Override
    public E selectByPrimaryKey(PK id) {
        ValidateUtil.checkNotNull(id, "条件不能为空");
        return this.baseMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageBean<E> page(C example) {
        ValidateUtil.checkNotNull(example, "条件不能为空");
        if (null != example.getOffSet() && null != example.getPageSize()) {
            PageInfo pageInfo = PageInfo.fromOffsetAndSize(example.getOffSet(), example.getPageSize());
            return new PageBean<>(pageInfo.getPageNo(), example.getPageSize(), this.selectByExample(example),
                    this.countByExample(example));
        }
        return new PageBean<>(this.selectByExample(example), this.countByExample(example));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByPrimaryKeySelective(E record) {
        ValidateUtil.checkNotNull(record, "更新数据不能为空");
        validateUpdate(record);
        ValidateUtil.isTrue(this.baseMapper.updateByPrimaryKeySelective(record) == 1, "更新数据有误");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByExampleSelective(E record, C example) {
        ValidateUtil.checkNotNull(record, "更新数据不能为空");
        ValidateUtil.checkNotNull(example, "更新数据条件不能为空");
        validateUpdate(record);
        return this.baseMapper.updateByExampleSelective(record, example);
    }

    public void validateInsert(E record) {

    }

    public void validateUpdate(E record) {

    }
}
